﻿Imports System.Xml
Imports System.Xml.Serialization

Namespace Maps.Geocode.Google

    Public Enum StatusType
        OK
        ZERO_RESULTS
        OVER_QUERY_LIMIT
        REQUEST_DENIED
        INVALID_REQUEST
    End Enum

    Public Enum ResultType
        street_address
        route
        intersection
        political
        country
        administrative_area_level_1
        administrative_area_level_2
        administrative_area_level_3
        colloquial_area
        locality
        sublocality
        neighborhood
        premise
        subpremise
        postal_code
        natural_feature
        airport
        park
        point_of_interest
        street_number
        bus_station
        transit_station
        establishment
        police
        museum
        subway_station
        hospital
        stadium
        health
    End Enum

    Public Enum LocationType
        ROOFTOP
        RANGE_INTERPOLATED
        GEOMETRIC_CENTER
        APPROXIMATE
    End Enum

    Public Class GeocodeResponse

        Public status As StatusType
        Public result As Result

        Public Shared Function GetData(ByVal Address As String) As GeocodeResponse

            Return UrlToClass(String.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=true", Address))

        End Function
        Public Shared Function GetDataLookup(ByVal Lat As Double, ByVal Lng As Double) As GeocodeResponse


            Return UrlToClass(String.Format("http://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}&sensor=true", UI.Print.NumStr(Lat), UI.Print.NumStr(Lng)))

        End Function

        Private Shared Function UrlToClass(ByVal Url As String) As GeocodeResponse

            Dim HttpClient As New System.Net.WebClient

            HttpClient.Encoding = System.Text.UTF8Encoding.UTF8

            Dim DataXml As String = HttpClient.DownloadString(Url)
            Dim reader As IO.StringReader = New IO.StringReader(DataXml)
            Dim serializer As XmlSerializer = New XmlSerializer(GetType(GeocodeResponse))

            Return serializer.Deserialize(reader)

        End Function

    End Class
    Public Class Result

        <XmlElementAttribute("type")> _
        Public type As New List(Of ResultType)

        Public formatted_address As String

        <XmlElementAttribute("address_component")> _
        Public address_component As New List(Of AddressComponent)

        Public geometry As Geometry


    End Class
    Public Class Geometry

        Public location As Location

        Public location_type As LocationType

        Public viewport As LocationBox

        Public bounds As LocationBox
    End Class
    Public Class Location

        Public lat As Double

        Public lng As Double

    End Class
    Public Class LocationBox

        Public southwest As Location

        Public northeast As Location

    End Class

    Public Class AddressComponent

        Public long_name As String

        Public short_name As String

        <XmlElementAttribute("type")> _
        Public type As New List(Of ResultType)

    End Class

End Namespace